package com.sinosoft.weixin.controller;

import com.sinosoft.weixin.controller.vo.ResultMsgVO;
import com.sinosoft.weixin.controller.vo.RiskDutyVO;
import com.sinosoft.weixin.service.MainRiskDutyQryService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by doney_mac on 2017/12/24.
 */

@RestController
@RequestMapping("api/riskDuty")
public class RiskDutyCheckContoller {

    private Logger mLogger = Logger.getLogger(RiskDutyCheckContoller.class);

    @Autowired
    private MainRiskDutyQryService service;

    @RequestMapping(value="check",method = RequestMethod.POST)
    public Object checkRiskDutyCheck(RiskDutyVO riskDutyVO){
        List<String> lstMain = riskDutyVO.getMainRiskDuty();
        String tSubRiskDutyCode = riskDutyVO.getSubRiskDuty();
        ResultMsgVO mVO=null;
        try{
            if(riskDutyVO==null){
                mLogger.info("责任校验信息失败!传入参数不能为空!");
                return ResultMsgVO.resultFail("责任校验信息失败!传入参数不能为空!");
            }
            mLogger.info("需要查询的附加险编码为:"+tSubRiskDutyCode);
            //校验判断传入的附加险校验编码不能为空!
            if(tSubRiskDutyCode==null||"".equals(tSubRiskDutyCode)){
                mLogger.info("责任校验信息失败!传入校验的责任编码不能为空,请检查参数!");
                return ResultMsgVO.resultFail("责任校验信息失败!传入校验的责任编码不能为空,请检查参数!");
            }
            //根据附加险编码查询对应的主险编码集合
            List<Map<String,Object>> lstMainDutyCode=service.qryMainRiskDuty(tSubRiskDutyCode);
            //查不到主险编码集合说明传入的责任编码为主险
            if(lstMainDutyCode==null || lstMainDutyCode.size() == 0){
                return ResultMsgVO.resultSucc();
            }
            if(lstMain==null&&lstMainDutyCode.size()>0){
                return ResultMsgVO.resultFail("附加责任不能单独添加,请先添加主险!");
            }
            //mLogger.info("主险编码列表:"+lstMain.);
            for (String m:lstMain) {
                for (Map map:lstMainDutyCode) {
                    String tMainDutyCode=(String) map.get("maindutycode");
                    if(tMainDutyCode.equals(m)){
                        return ResultMsgVO.resultSucc();
                    }
                }

            }
            mVO=ResultMsgVO.resultFail("附加责任:"+tSubRiskDutyCode+"不能单独添加,请先首先添加主险信息("+lstMainDutyCode.toString()+")!");
        }catch(Exception e){
            e.printStackTrace();
            return ResultMsgVO.resultFail("责任校验信息失败!原因:"+e.getMessage());
        }
            return mVO;
    }
}
